package com.ihm.projetihm_v1.activity.preferences;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import com.ihm.projetihm_v1.R;

public class PreferencesFragment
		extends PreferenceFragment
		implements OnSharedPreferenceChangeListener
{
	//+++++++++++//
	// CONSTANTS //
	//+++++++++++//
	
	public static enum PREFERENCE_ENUM
	{
		CONNEXION_IP {
			@Override
			public String getKey() {
				return "pref_key_connexion_ip";
			}
			@Override
			public String getDefaultValue() {
				return "NO IP";
			}
		},
		CONNEXION_PORT {
			@Override
			public String getKey() {
				return "pref_key_connexion_port";
			}
			@Override
			public String getDefaultValue() {
				return "NO PORT";
			}
		};
		public abstract String getKey();
		public abstract String getDefaultValue();
		public static PREFERENCE_ENUM findPreferenceByKey(String key)
		{
	        for (PREFERENCE_ENUM prefEnum : PREFERENCE_ENUM.values())
	        {
	        	if (prefEnum.getKey().equals(key))
	        	{
	        		return prefEnum;
	        	}
	        }
	        return null;
		}
	}
	

	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//

	private Map<PREFERENCE_ENUM, Preference> mMapPreferences;
	

	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//
	
	public PreferencesFragment()
	{
		this.mMapPreferences = new HashMap<PREFERENCE_ENUM, Preference>();
	}
	

	//+++++++++//
	// METHODS //
	//+++++++++//
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        
        for (PREFERENCE_ENUM prefEnum : PREFERENCE_ENUM.values())
        {
        	Preference prefObject = this.getPreferenceScreen().findPreference(prefEnum.getKey());
        	this.mMapPreferences.put(prefEnum, prefObject);
        }
    }

	@Override
	public void onSharedPreferenceChanged(
			SharedPreferences sharedPreferences,
			String key)
	{
		PREFERENCE_ENUM pref = PREFERENCE_ENUM.findPreferenceByKey(key);
		
		String summary = sharedPreferences.getString(key, pref.getDefaultValue());
		Preference prefObject = this.mMapPreferences.get(pref);
		
		prefObject.setSummary(summary);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		// Setup the initial values
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this.getActivity());
		
		for (Entry<PREFERENCE_ENUM, Preference> entry : this.mMapPreferences.entrySet())
		{
			String summary = sharedPreferences.getString(
					entry.getKey().getKey(),
					entry.getKey().getDefaultValue()
				);
			entry.getValue().setSummary(summary);
		}
		
		// Set up a listener whenever a key changes            
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onPause()
	{
	    super.onPause();
	    // Unregister the listener whenever a key changes            
	    getPreferenceScreen().getSharedPreferences()
	    		.unregisterOnSharedPreferenceChangeListener(this);    
	}

	
	//++++++++++++++++//
	// STATIC METHODS //
	//++++++++++++++++//
}
